using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace InvadersEx02.ObjectModel
{
    public class Enemy : Sprite
    {
        public event EventHandler EnemyReachedBottom;

        private enum eMovingDirection
        {
            Left,
            Right
        }

        private float m_MoveInterval = (float)TimeSpan.FromSeconds(0.5f).TotalSeconds;
        private float m_TimeToNextMove;
        private float m_LeftDownJumpBound;
        private float m_RightDownJumpBound;
        private eMovingDirection m_MovingDirection;

        public float LeftDownJumpBound
        {
            get { return m_LeftDownJumpBound; }
            set { m_LeftDownJumpBound = value; }
        }

        public float RightDownJumpBound
        {
            get { return m_RightDownJumpBound; }
            set { m_RightDownJumpBound = value; }
        }

        public Enemy(Game game, string i_AssetName)
            : base(game, i_AssetName)
        {
            m_MovingDirection = eMovingDirection.Right;
        }

        public override void Update(GameTime gameTime)
        {
            m_TimeToNextMove -= (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (m_TimeToNextMove <= 0)
            {
                m_TimeToNextMove = m_MoveInterval;

                switch (m_MovingDirection)
                {
                    case eMovingDirection.Right:
                        if (Position.X + (Width / 2) - RightDownJumpBound <= 0.1)
                        {
                            moveRight();
                        }
                        else
                        {
                            reachEdgeAction();
                        }
                        break;
                    case eMovingDirection.Left:
                        if (Position.X - (Width / 2) - LeftDownJumpBound >= 0.1)
                        {
                            moveLeft();
                        }
                        else
                        {
                            reachEdgeAction();
                        }
                        break;
                }
            }

            base.Update(gameTime);
        }

        protected void OnEnemyReachedBottom()
        {
            if (EnemyReachedBottom != null)
            {
                EnemyReachedBottom(this, new EventArgs());
            }
        }

        private void moveRight()
        {
            Position = new Vector2(Position.X + (Width / 2), Position.Y);
        }

        private void moveLeft()
        {
            Position = new Vector2(Position.X - (Width / 2), Position.Y);
        }

        private void moveDown()
        {
            Position = new Vector2(Position.X, Position.Y + (Height / 2));
            m_MoveInterval -= 0.07f * m_MoveInterval;
        }

        private void reachEdgeAction()
        {
            moveDown();

            if (Position.Y + Height + (Height / 2) > GraphicsDevice.Viewport.Height)
            {
                OnEnemyReachedBottom();
            }
            else
            {
                toggleMovingDirection();
            }
        }

        private void toggleMovingDirection()
        {
            if (m_MovingDirection == eMovingDirection.Left)
            {
                m_MovingDirection = eMovingDirection.Right;
            }
            else
            {
                m_MovingDirection = eMovingDirection.Left;
            }
        }
    }
}